/*
 * Copyright (C) 2009 Umin (umin90@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include "gui.h"

image_p display_menu_images[10],display_playing_images[7],display_common_images[4];
image_p fontMask;

PFont font,font2;

char display_common_strings[4][10];
nge_timer* battery_timer;



pointf point_common_strings[]={ 
    {40,4},  //CPU
    {192,4}, //SLEEP
    {312,4},  //DATE
    {400,} //TIME
};

pointf point_playing_strings[]={ 
    {105,249},  //ALBUM
    {171,226},  //CODEC
    {89,202}    //PLAY TIME
};


pointf point_menu_images[]={
  {0,0},
    {50,20},
    {201,20},
    {351,20},
    {50,87},
    {201,87},
    {351,87},
    {50,150},
  {201,151},
  {351,151}
};

pointf point_COMMON_images[]={
    {450,2},
    {450,2},
    {450,2},
    {450,2}
};

pointf point_playing_images[]={
    {0,0},    //bg
    {0,185},  //down
    {0,0},    //bgMask
    {4,196},  //coverMask
    {258,206},//slip
    {84,197}, //vol
    {90,226}, //timeMask
    {6,198}   //COVER
};

image_p loadPNGImage(const char * path){
    image_p tmp;
    if(fileExists(path)>-1)
        tmp=image_load(path,DISPLAY_PIXEL_FORMAT_8888,1);
    else{
        u_error("image not exists:",path);
        return;
    }
    if(!tmp)
        u_error("error load image:",path);
    else
        return tmp;
}

#define blitImage(image,x,y) ImageToScreen(image,x,y)

void Gui_loadCommonImages(){  
                    #ifdef DEGUG
                    u_log("loading common images...\n");
                    #endif
  display_common_images[0]=loadPNGImage("SKINS/DEFAULT/COMMON/BATTERY1.PNG");
  display_common_images[1]=loadPNGImage("SKINS/DEFAULT/COMMON/BATTERY2.PNG");
  display_common_images[2]=loadPNGImage("SKINS/DEFAULT/COMMON/BATTERY3.PNG");
  display_common_images[3]=loadPNGImage("SKINS/DEFAULT/COMMON/BATTERY4.PNG");
}

void Gui_loadPlayingImages(){  
                   #ifdef DEGUG
                   u_log("loading playing images...\n");
                   #endif
  display_playing_images[0]=loadPNGImage("SKINS/DEFAULT/playing/bg.PNG");
  display_playing_images[1]=loadPNGImage("SKINS/DEFAULT/playing/down.PNG");
  display_playing_images[2]=loadPNGImage("SKINS/DEFAULT/playing/bgMask.PNG");
  display_playing_images[3]=loadPNGImage("SKINS/DEFAULT/playing/coverMask.PNG");
  display_playing_images[4]=loadPNGImage("SKINS/DEFAULT/playing/slip.PNG");
  display_playing_images[5]=loadPNGImage("SKINS/DEFAULT/playing/vol.PNG");
  display_playing_images[6]=loadPNGImage("SKINS/DEFAULT/playing/timeMask.PNG");
}
void Gui_loadMenuImages(){  
                  #ifdef DEGUG
                  u_log("loading menu images...\n");
                  #endif
    display_menu_images[0]=loadPNGImage("SKINS/DEFAULT/main/bg.PNG");
    display_menu_images[1]=loadPNGImage("SKINS/DEFAULT/main/playing.PNG");
    display_menu_images[2]=loadPNGImage("SKINS/DEFAULT/main/lib.PNG");
    display_menu_images[3]=loadPNGImage("SKINS/DEFAULT/main/eq.PNG");
    display_menu_images[4]=loadPNGImage("SKINS/DEFAULT/main/share.PNG");
    display_menu_images[5]=loadPNGImage("SKINS/DEFAULT/main/sleep.PNG");
    display_menu_images[6]=loadPNGImage("SKINS/DEFAULT/main/settings.PNG");
    display_menu_images[7]=loadPNGImage("SKINS/DEFAULT/main/help.PNG");
    display_menu_images[8]=loadPNGImage("SKINS/DEFAULT/main/about.png");
    display_menu_images[9]=loadPNGImage("SKINS/DEFAULT/main/quit.png");
}

/******************************************************************************************/
/******************************************************************************************/

void display_blitBattery(){
    int percent;
    percent = scePowerGetBatteryLifePercent();
    if(scePowerIsBatteryExist()){
        if(scePowerIsBatteryCharging()){
            if(battery_timer->get_ticks(battery_timer)<=500lu){
                if(percent>=66){
                    blitImage(display_common_images[0],point_COMMON_images[0].x,point_COMMON_images[0].y);
                }else if(percent<66&&percent>=33){
                    blitImage(display_common_images[1],point_COMMON_images[1].x,point_COMMON_images[1].y);
                }
                else if(percent<33&&percent>=10){
                    blitImage(display_common_images[2],point_COMMON_images[2].x,point_COMMON_images[2].y);
                }
                else{
                    blitImage(display_common_images[3],point_COMMON_images[3].x,point_COMMON_images[3].y);
                }
            }else if(battery_timer->get_ticks(battery_timer)>1000lu){
                battery_timer->stop(battery_timer);
                battery_timer->start(battery_timer);
            }
        }else{
            if(percent>=66){
                blitImage(display_common_images[0],point_COMMON_images[0].x,point_COMMON_images[0].y);
            }else if(percent<66&&percent>=33){
                blitImage(display_common_images[1],point_COMMON_images[1].x,point_COMMON_images[1].y);
            }else if(percent<33&&percent>=10){
                blitImage(display_common_images[2],point_COMMON_images[2].x,point_COMMON_images[2].y);
            }else{
                blitImage(display_common_images[3],point_COMMON_images[3].x,point_COMMON_images[3].y);
            }
        }
    }
}

/******************************************************************************************/
/******************************************************************************************/

void display_blitImage_menu(int heighlight){
    blitImage(display_menu_images[0],point_menu_images[0].x,point_menu_images[0].y);
    blitImage(display_menu_images[heighlight],point_menu_images[heighlight].x,point_menu_images[heighlight].y);
    display_blitBattery();
}

void display_blitImage_playing(){
	int j;
    for(j=0;j<7;j++)
        blitImage(display_playing_images[j],point_playing_images[j].x,point_playing_images[j].y);
}

/******************************************************************************************/
/******************************************************************************************/

void display_printString_common(){

    /* Draw CPU Clock */
    sprintf(display_common_strings[0],"CPU:%d",scePowerGetCpuClockFrequency());  
    font_setcolor(font,WHITE);
    font_drawtext(font,display_common_strings[0],strlen(display_common_strings[0]),fontMask,point_common_strings[0].x,point_common_strings[0].y,0);
  
    sys_time_t td;
    sys_localtime(&td);
  
    //Draw Time 
    sprintf(display_common_strings[2],"%.2d:%.2d",td.hour,td.minutes);
    font_setcolor(font,WHITE);
    font_drawtext(font,display_common_strings[2],strlen(display_common_strings[2]),fontMask,point_common_strings[3].x,point_common_strings[3].y,0);

    //Draw Year and Date
    sprintf(display_common_strings[3],"%4d/%2d/%2d",td.year,td.month,td.day);
    font_setcolor(font,WHITE);
    font_drawtext(font,display_common_strings[3],strlen(display_common_strings[3]),fontMask,point_common_strings[2].x,point_common_strings[2].y,0);
  
    /* Blit Font Image to Screen */
    //Clear Image
    ImageToScreen(fontMask,0,0);
}

/******************************************************************************************/
/******************************************************************************************/

void Gui_init(){
                  #ifdef DEGUG
                  u_log("Init GUI...\n");
                  #endif
  
    battery_timer = timer_create();
    battery_timer->start(battery_timer);
    fontMask = image_create(480, 272, DISPLAY_PIXEL_FORMAT_8888);
    
                  #ifdef DEGUG
                  u_log("loading fonts...\n");
                  #endif
    font = create_font_hzk("Fonts/GBK12","Fonts/ASC12",12,DISPLAY_PIXEL_FORMAT_8888);
	font2 = create_font_freetype("Fonts/FZXH.TTF",12,DISPLAY_PIXEL_FORMAT_8888);
  
    Gui_loadCommonImages();
}

void Gui_freeImageCommon(){  
                   #ifdef DEGUG
                   u_log("Free common images...\n");
                   #endif
	int j;
    for(j=0;j<3;j++)
    image_free(display_common_images[j]);
}

void Gui_freeImageMenu(){
                   #ifdef DEGUG
                   u_log("Free menu images...\n");
                   #endif
	int j;
    for(j=0;j<9;j++)
    image_free(display_menu_images[j]);
}

void Gui_freeImagePlaying(){
                  #ifdef DEGUG
                  u_log("Free playing images...\n");
                  #endif
	int j;
    for(j=0;j<6;j++)
    image_free(display_playing_images[j]);
}

void Gui_distroyFonts(){
          #ifdef DEGUG
          u_log("Destory font...\n");
          #endif
  font_destory(font);
}

/******************************************************************************************/
/******************************************************************************************/

void Gui_playing(){
    BeginScene(1);
    display_blitImage_playing();
    display_printString_common();
    EndScene();
}

void Gui_menu(){
    BeginScene(1);
    extern selection_selector;
    
             #ifdef DEGUG
             #ifdef DEBUG_LOOP
             u_log("\nBlit menu images...\n");
             #endif
             #endif
    display_blitImage_menu(selection_selector);
             #ifdef DEGUG
             #ifdef DEBUG_LOOP
             u_log("Print strings...\n");
             #endif
             #endif
    display_printString_common();
    display_printString_common();
    EndScene();
}

void Gui_library(){
    BeginScene(1);
    extern selection_selector;
  
    EndScene();
}


